local daweidai = fk.CreateSkill {
  name = "qunxian3__liangzi_daweidai",
  tags = { Skill.Compulsory }
}

Fk:loadTranslationTable {
  ["qunxian3__liangzi_daweidai"] = "大胃袋",
  ["$qunxian3__liangzi_daweidai1"] = "良子大胃袋，属实挺带派。",
  ["$qunxian3__liangzi_daweidai2"] = "胃袋圣经",
  [":qunxian3__liangzi_daweidai"] = "锁定技，当你的受到伤害/回复体力/摸牌/弃牌时，将此数值改为X。你的体力值未变化的回合结束时，你失去1点体力。（x为你的体力值的个位数）",
  ["#qunxian3__liangzi_daweidai-choose_discard"] = "大胃袋：请选择 %arg 张牌进行弃置",
}

daweidai:addEffect(fk.DetermineDamageInflicted, {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill("qunxian3__liangzi_daweidai")
  end,
  on_use = function(self, event, target, player, data)
    data.damage = player.hp % 10
  end
})

daweidai:addEffect(fk.PreHpRecover, {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill("qunxian3__liangzi_daweidai")
  end,
  on_use = function(self, event, target, player, data)
    data.num = player.hp % 10
  end
})

daweidai:addEffect(fk.BeforeDrawCard, {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill("qunxian3__liangzi_daweidai")
  end,
  on_use = function(self, event, target, player, data)
    data.num = player.hp % 10
  end
})

daweidai:addEffect(fk.BeforeCardsMove, {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill("qunxian3__liangzi_daweidai") then
      for _, move in ipairs(data) do
        if move.from == player and move.moveReason == fk.ReasonDiscard then
          return #move.moveInfo ~= player.hp % 10 and move.skillName ~= daweidai.name
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cids = {}
    for _, move in ipairs(data) do
      if move.from == player and move.moveReason == fk.ReasonDiscard then
        for index, info in ipairs(move.moveInfo) do
          table.insertIfNeed(cids, info.cardId)
        end
      end
    end
    room:cancelMove(data, cids)
    local num = player.hp % 10
    local discards = room:askToDiscard(player, {
      min_num = num,
      max_num = num,
      include_equip = true,
      skill_name = daweidai.name,
      cancelable = true,
      prompt = "#qunxian3__liangzi_daweidai-choose_discard:::" .. num,
    })
    if #discards == 0 then
      if #player:getCardIds("he") < num then
        discards = player:getCardIds("he")
      else
        discards = table.random(player:getCardIds("he"), num)
      end
      room:throwCard(discards, daweidai.name, player, player)
    end
  end
})

daweidai:addEffect(fk.TurnEnd, {
  anim_type = "negative",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill("qunxian3__liangzi_daweidai") and
        player.hp == player:getMark("qunxian3__liangzi_daweidai_hp")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:loseHp(player, 1, "qunxian3__liangzi_daweidai")
  end
})
daweidai:addEffect(fk.TurnStart, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill("qunxian3__liangzi_daweidai")
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "qunxian3__liangzi_daweidai_hp", player.hp)
  end
})

return daweidai
